package com.ngsky.ana.server;

import com.ngsky.ana.conf.AppConstants;
import com.ngsky.ana.utils.DateUtil;
import com.ngsky.ana.utils.ImageUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;

import static com.ngsky.ana.server.AnalyzeServer.fd;

/**
 * @Author syx
 * @Desc
 * @Create 2020/6/16 14:23
 * <p>
 * * 写入数据
 * *
 * * 12-0  第一张图片
 * * C://test/test.png 图片路径
 * * 0:23.23% 分析数据
 * * 23.23%
 * * 23.23%
 * * 23.23%
 * * 23.23%
 * * 23.23%
 * * 23.23%
 * * 23.23%
 * * 23.23%
 * * 23.23%
 */
public class AnalyzeTask implements Runnable {
    private int count;
    private int curIndex;
    private String imgFile;

    public AnalyzeTask(int count, int curIndex, String imgFile) {
        this.imgFile = imgFile;
        this.count = count;
        this.curIndex = curIndex;
    }

    @Override
    public void run() {
        try {
            AnalyzeServer server = new AnalyzeServer();
            double[] color = server.analyze(imgFile);
            // 将结果写入文件 - date
            String dirPath = AppConstants.getDBPath() + "\\" + DateUtil.getMonth(new Date());
            File saveDir = new File(dirPath);
            if (!saveDir.exists()) {
                saveDir.mkdirs();
            }
            String saveFile = dirPath + "\\" + "." + imgFile.hashCode();
            ImageUtil.writeDb(saveFile, count, curIndex, imgFile, color);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
